package com.example.swaggertest.controller;


import com.example.swaggertest.common.ResponseResult;
import com.example.swaggertest.model.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassDescription:
 * @Author：李白
 * @Date：2023/5/22 15:01
 */
@Api(value = "用户数据操作类",tags = {"用户数据操作接口"})
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 模拟数据库
     */
    static Map<Integer, UserInfo> users = new HashMap<>();

    /**
     * 模拟自增主键
     */
    static Integer id = 1;

    /**
     * 查询所有数据
     * @return
     */
    @ApiOperation(value = "查询用户数据", notes = "查询所有用户数据")
    @GetMapping
    public ResponseResult getUserInfoList(){
        List<UserInfo> userInfoList = new ArrayList<>(users.values());
        return ResponseResult.success(userInfoList);
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询数据", notes = "根据id查询数据信息")
//    @ApiParam(name = "id", value = "用户id", required = true)
//    @ApiImplicitParam(name = "id", value = "用户id", dataType = "Integer", paramType = "query",example = "libai123", required = true)
    @GetMapping("/{id}")
    public ResponseResult getUserInfoById(@PathVariable("id") Integer id){
        return ResponseResult.success(users.get(id));
    }

    /**
     * 新增数据
     * @param userInfo
     * @return
     */
//    @ApiIgnore//用在类或方法上，可以不被swagger显示在页面上
//    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = UserInfo.class)})
    @ApiOperation(value = "新增数据", notes = "新增用户数据信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true)
    })
    @PostMapping
    public ResponseResult postUserInfo(UserInfo userInfo){
        userInfo.setId(id++);
        users.put(userInfo.getId(),userInfo);
        System.out.println(users);
        return ResponseResult.success();
    }

    /**
     * 更新数据
     * @param userInfo
     * @return
     */
    @ApiOperation(value = "更新数据", notes = "更新用户数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true)
    })
    @PutMapping
    public ResponseResult putUserInfo(UserInfo userInfo){
        UserInfo userInfo1 = users.get(userInfo.getId());
        userInfo1.setPassword(userInfo.getPassword());
        userInfo1.setUsername(userInfo.getUsername());
        return ResponseResult.success(userInfo1);
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @ApiOperation(value = "删除数据", notes = "根据id删除用户数据")
    @ApiImplicitParam(name = "id", value = "用户id", required = true)
    @DeleteMapping("/{id}")
    public ResponseResult deleteUserInfo(@PathVariable("id") Integer id){
        users.remove(id);
        return ResponseResult.success();
    }




}